<template>
  <div>

    <el-form ref="form" :model="form" label-width="120px">
      <el-form-item label="品类名称：">
        <el-input v-model="productTypeVo.name"></el-input>
      </el-form-item>

      <el-form-item label="品类顺序：">
        <template>
          <el-input-number v-model="productTypeVo.priority" :min="1" :max="5" label="品类顺序：" @change="handleChange"></el-input-number>
        </template>
      </el-form-item>

      <el-form-item label="子品类：">
        <el-tag
          v-for="tag in dynamicTags"
          :key="tag"
          closable
          :disable-transitions="false"
          @close="handleClose(tag)">
          {{ tag }}
        </el-tag>
        <el-input
          v-if="inputVisible"
          ref="saveTagInput"
          v-model="inputValue"
          class="input-new-tag"
          @keyup.enter.native="handleInputConfirm"
          @blur="handleInputConfirm">
        </el-input>
        <el-button v-else class="button-new-tag" @click="showInput">+ New Tag</el-button>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即创建</el-button>
        <el-button>取消</el-button>
      </el-form-item>

    </el-form>

  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "AddProductType",
  data() {
    return {
      form: "",
      productTypeVo: {
        name: "",
        priority: "",
        subClasses: []
      },
      dynamicTags: ['标签一', '标签二', '标签三'],
      inputVisible: false,
      inputValue: ''
    }
  },
  methods: {
    onSubmit: function () {
      this.dynamicTags.forEach(str => {
        let subClass = {
          name: str
        }
        this.productTypeVo.subClasses.push(subClass)
      });
      axios.post('/productType/insert', {
        productTypeVo: this.productTypeVo
      }).then((res) => {
        if (res.data.code == 200) {
          this.$message({
            type: 'success',
            message: '添加成功'
          });
          this.$router.push({
            path: "productType"
          })
        } else {
          this.$message({
            type: 'warn',
            message: '系统繁忙，请稍后重试！'
          });
        }
      }).catch((error) => {
        console.log(error);
      });
    },
    handleChange(value) {
      console.log(value);
    },
    handleClose(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
    },

    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },

    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.dynamicTags.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = '';
    }
  }
}
</script>

<style scoped>
.el-form {
  width: 540px;
  margin: 30px 20px 20px 20px;
}

.el-tag + .el-tag {
  margin-left: 10px;
}

.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}

.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
